namespace Infrastructure.WebApi
{
    public class ResponseBase<T>
    {
        public T Data { get; set; }
        public ApiCode Code { get; set; } = ApiCode.Success;
        public string Msg { get; set; } = Result.DefaultSuccessMessage;
        public string Rid { get; set; } = Guid.NewGuid().ToString("N");
    }
    public class ResponseBase : ResponseBase<dynamic>
    {
        public ResponseBase()
        {
            Msg = Result.DefaultSuccessMessage;
        }
        public ResponseBase(string msg, ApiCode code)
        {
            Msg = msg;
            Code = code;
        }
    }
    /// <summary>
    /// 返回分页对象
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Page<T>
    {
        public List<T> List { get; set; }
        public int TotalCount { get; set; }
    }
    public enum ApiCode
    {
        Error = -1,//普通错误
        Success = 200,//业务成功
        RequestError = 400,//请求错误
        Unauthorized = 401,//暂无权限
    }

    public class Result
    {
        public const string DefaultSuccessMessage = "成功！";
        public const string DefaultErrorMessage = "失败！";

        #region 成功
        public static ResponseBase Success(string message = DefaultSuccessMessage)
        {
            return new ResponseBase { Msg = message };
        }
        public static ResponseBase<T> Success<T>(T data, string message = DefaultSuccessMessage)
        {
            //eg:Result.Success<string>("字符串");Result.Success<User>(user);
            return new ResponseBase<T> { Data = data, Msg = message };
        }
        #endregion

        #region 失败
        public static ResponseBase Fail(string message = DefaultErrorMessage)
        {
            return new ResponseBase { Msg = message, Code = ApiCode.Error };
        }
        public static ResponseBase Fail(string message = DefaultErrorMessage, ApiCode code = ApiCode.Error)
        {
            return new ResponseBase { Msg = message, Code = code };
        }
        #endregion
    }
}
